import time, threading

Event = threading.Event	# gevent follows this API for Event

#class AsyncResult( threading._Event ): pass
#AsyncResult = threading.Event #gevent's AsyncResult is almost the same as a threading.Event

class AsyncResult(object):
  def __init__(self):
    self.value = None
    self._set = False
  def __call__(self,value): self.set(value)
  def set(self,value):
    self.value = value
    self._set = True

  def get(self, block=True, timeout=None):
    if self._set: return self.value
    elif block:
      if timeout:
        start = time.time()
        now = start
        while not self._set and now-start < timeout:
          time.sleep( 0.01 )
          now = time.time()
      else:
        while not self._set: time.sleep(0.03)
      return self.value

    else: raise RuntimeError